package edu.gatech.oops.servlets;

import java.util.Random;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CreateLab
 */
public class CreateLab extends HttpServlet implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateLab() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException {
		String CourseName = request.getParameter("courseName");
		String labName = request.getParameter("labName");
		String labTime = request.getParameter("labTime");
		String dueDate = request.getParameter("dueDate");
		String teamLimit = request.getParameter("teamLimit");
		String type = request.getParameter("type");
		
		Course C1 = new Course(CourseName , labName);
		FileOutputStream f_out = new FileOutputStream("/home/pushkara/workspace/LabAssistan/courses.data");
		ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
		obj_out.writeObject ( C1 );
		obj_out.close();
		f_out.close();
		FileInputStream f_in = new FileInputStream("/home/pushkara/workspace/LabAssistan/courses.data");
		ObjectInputStream obj_in = new ObjectInputStream (f_in);
		Object course = obj_in.readObject();
		
			// TODO Auto-generated catch block
			// Cast object to a Vector
			Course myCourse = (Course) course;

			// Do something with vector....
		
		
		String name = myCourse.getCourseName();
		System.out.println(name);
		File file = new File("/home/pushkara/workspace/LabAssistan/"
				+ CourseName + ".txt");
		BufferedReader read = new BufferedReader(new FileReader(file));
		int studentCounter = 0;
		while (read.readLine() != null)
			studentCounter++;

		File lFile = new File("/home/pushkara/workspace/LabAssistan/" +CourseName +"_"+ labName
				+ ".txt");
		int tLimit = Integer.parseInt(teamLimit);
		int numTeams = studentCounter / tLimit;
		int extras = studentCounter % tLimit;
		String[] students = new String[studentCounter];
		
		String[][] team;
		if (studentCounter % tLimit == 0)
			team = new String[numTeams][tLimit];
		else
			team = new String[numTeams][tLimit + 1];

		if (type.equals("random")) {
			

			read = new BufferedReader(new FileReader(file));
			String line = read.readLine();
			int s = 0;
			while (line != null) {
				students[s] = line;
				s++;
				line = read.readLine();
			}
			
			Random rand = new Random();
			for (int i = 0; i < team.length; i++) {
				for (int j = 0; j < tLimit; j++) {
					int place = rand.nextInt(studentCounter);
					while (students[place] == null)
						place = rand.nextInt(studentCounter);
					team[i][j] = students[place];
					students[place] = null;
				}
			}
			int teamCounter = 0;
			for (int i = 0; i < students.length; i++) {
				if (students[i] != null) {
					team[teamCounter][tLimit] = students[i];
					students[i] = null;
					teamCounter++;

				}
			}
			
			BufferedWriter writer = new BufferedWriter(new FileWriter(lFile, true));
			writer.write(CourseName + "," +labName + "," +labTime +  "," +dueDate+ "\n");
			for (int i = 0; i < team.length; i++) {
				writer.write("Team" + (i + 1) +" ");
				for (int j = 0; j < team[i].length; j++) {
					if (team[i][j] != null)
						writer.write(team[i][j] + ",");
						//System.out.print(team[i][j] + " ");
				}
				writer.write("\n");
				//System.out.println();
			}
			writer.close();
		}
		else if (type.equals("select")){
			
			Lab L1 = new Lab(CourseName, labName, numTeams, tLimit);
			FileOutputStream lab_out = new FileOutputStream("/home/pushkara/workspace/LabAssistan/"+CourseName+"_"+labName+".data");
			ObjectOutputStream object_out = new ObjectOutputStream(lab_out);
			object_out.writeObject ( L1 );
			object_out.close();
			lab_out.close();
			
			
			/*BufferedWriter writer = new BufferedWriter(new FileWriter(lFile, true));
			writer.write(CourseName + "," +labName + "," +labTime +  "," +dueDate+ "\n");
			for (int i = 0; i < team.length; i++) {
				writer.write("Team" + (i+1) + " ");
				for (int j = 0; j < team[i].length; j++) {
					writer.write("blank ");
				}
				writer.write("\n");
			}
			writer.close();*/
			
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
